package com.esa.servlet.choice;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xml.sax.SAXException;

import com.esa.dbi.impl.ChoiceDBImpl;
import com.esa.dbi.impl.CourseDBImpl;
import com.esa.dbi.impl.StudentDBImpl;
import com.esa.javabean.Choice;
import com.esa.javabean.Course;
import com.esa.tois.TransDrop;

public class DropChoiceServlet extends HttpServlet {
	public void doGet(HttpServletRequest request,HttpServletResponse response)
	throws ServletException,IOException{
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		this.doPost(request, response);
		out.flush();
		out.close();
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response)
	throws ServletException,IOException{
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		
		String acname=(String)request.getSession().getAttribute("acname");
		
		StudentDBImpl sdbi=new StudentDBImpl();
		String sno=sdbi.getByAcname(acname).getSno();
		String cno=request.getParameter("cno");
		ChoiceDBImpl cdbi=new ChoiceDBImpl();
		CourseDBImpl cD=new CourseDBImpl();
		Choice ch=cdbi.getChoice(cno, sno);
		Course c=cD.getCourseByCno(cno);
		List<Choice> list=new ArrayList<Choice>();
		TransDrop tdc=new TransDrop();
		if(cno.charAt(0)!='1'){
			list.add(ch);
			try {
				tdc.trans(list);
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			
		}
		cdbi.deleteChoice(cno, sno);
		
		//request.setAttribute("acname",acname);
		response.sendRedirect("ShowMyChoicesServlet");
	//System.out.println("DeleteUserServlet line 30....");

		out.flush();
		out.close();
	}
}
